<?php


namespace app\admin\controller;


use app\admin\model\Admin;
use app\admin\model\Admin as AdminModel;
use think\Controller;
use think\facade\Validate;
use think\Session;

class Index extends Controller
{
    //登录主页
    public function index()
    {
        if (!session('username')) {
            $this->redirect('login');
        } else {
            return $this->fetch();
        }
    }

    //用户列表
    public function userList()
    {
        if (!session('username')) {
            $this->redirect('login');
        } else {
            $adminModel = new AdminModel();
            $data_list = $adminModel->order('id desc')->paginate(10);
            $page = $data_list->render(); //分页
            $this->assign('data_list', $data_list);
            $this->assign('page', $page);
            return $this->fetch();
        }
    }


    //登录功能
    public function login()
    {
        if ($this->request->isPost()) {
            //1.获取数据
            $data = $this->request->post();

            //2.验证数据
            $validate = Validate::make([
                'username' => 'require',
                'password' => 'require'
            ], [
                'username.require' => '用户名不能为空',
                'password.require' => '密码不能为空'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            //数据验证
            $data['password'] = md5($data['password']);//md5加密
            $result = Admin::login($data['username'], $data['password']);

            if ($result) {
                //登录成功
                session('username', $data['username']);

                $this->success('登录成功', url('index/index/index'));
            } else {
                //登录失败
                $this->error('登录失败', url('login'));
            }
        } else {
            //跳转到login.html
            return $this->fetch();
        }
    }

    //退出登录
    public function logout()
    {
        session(null);//退出清空session
        return $this->success('退出成功', url('login'));//跳转到登录页面
    }

    //注册功能
    public function Register()
    {
        if ($this->request->post()) {
            //1.获取数据
            $data = $this->request->post();

            //2.验证数据
            $validate = Validate::make([
                'username' => 'require',
                'nickname' => 'require',
                'password' => 'require',
                'conpass' => 'require|confirm:password',
                'email' => 'email'
            ], [
                'username.require' => '用户名不能为空',
                'nickname.require' => '昵称不能为空',
                'password.require' => '密码不能为空',
                'email.require' => '邮箱地址不能为空',
                'conpass' => '两次密码输入不一致'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $data['password'] = md5($data['password']);
            unset($data['conpass']);
            //3.数据验证
            if ($result = Admin::register($data)) {
                $this->success('注册成功');
                $this->redirect('login');
            }
        } else {
            return $this->fetch();
        }
    }

    // 新增用户
    public function add()
    {
        if ($this->request->isPost()) {
            //1.获取数据
            $data = $this->request->post();
            //2.验证数据
            $validate = Validate::make([
                'username' => 'require',
                'nickname' => 'require',
                'password' => 'require',
                'conpass' => 'require|confirm:password',
                'email' => 'email'
            ], [
                'username.require' => '用户名不能为空',
                'nickname.require' => '昵称不能为空',
                'password.require' => '密码不能为空',
                'email.require' => '邮箱地址不能为空',
                'conpass' => '两次密码输入不一致'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $data['password'] = md5($data['password']);
            unset($data['conpass']);
            //3.数据检验
            if ($result = Admin::register($data)) {
                $this->success('新增用户成功');
            }
        } else {
            $this->error('新增用户失败');
        }
    }

    //个人信息（修改当前用户的用户名）
    public function userInfo()
    {
        if ($this->request->post()) {
            //1.获取数据
            $data = $this->request->post();
            $newname = $data['username'];

            //2.验证数据
            $validate = Validate::make([
                'username' => 'require'
            ], [
                'username.require' => '用户名项不能为空'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            //3.数据检验
            if ($result = Admin::upUsername(session('username'), $newname)) {
                session(null);
                session('username', $newname);
                $this->success('修改用户名成功');
            }
        } else {
            $this->error('修改用户名失败');
        }
    }

    public function modifyPwd()
    {
        if ($this->request->post()) {
            //1.获取数据
            $data = $this->request->post();
            $newpwd = md5($data['password']);

            //2.验证数据
            $validate = Validate::make([
                'password' => 'require'
            ], [
                'password.require' => '新密码项不能为空'
            ]);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            //3.数据验证
            if ($result = Admin::upUserPw(session('username'), $newpwd)) {
                $this->success('修改当前用户密码成功');
            }
        } else {
            $this->error('修改当前用户密码失败');
        }
    }
}